﻿using System.Collections.Generic;
using Ebt.Mvvm;
using Ebt.Mvvm.Navigator;

namespace Windows_8_MVVM.ViewModels.Pages
{
    public class SearchPageModel : PageModel
    {
        private readonly RootModel _rootModel;
        private string _queryText;
        private bool _hasResults;
        private List<object> _resultList;

        public SearchPageModel(RootModel rootModel)
        {
            _rootModel = rootModel;
        }

        public string QueryText
        {
            get { return _queryText; }
            set { OnPropertyChange(ref _queryText, value); }
        }

        public bool HasResults
        {
            get { return _hasResults; }
            private set { OnPropertyChange(ref _hasResults, value); }
        }

        public List<object> ResultList
        {
            get { return _resultList; }
            set { OnPropertyChange(ref _resultList, value); }
        }

        public RootModel RootModel
        {
            get { return _rootModel; }
        }

        public void ProcessQueryText(string queryText)
        {
            if (string.IsNullOrWhiteSpace(queryText) || QueryText == queryText)
                return;
            HasResults = true;
            QueryText = queryText;
            ResultList = _rootModel.SearchData(queryText);
            HasResults = ResultList.Count > 0;
        }

        public override void OnNavigatedFrom(NavigationArgs navigationArgs)
        {
            base.OnNavigatedFrom(navigationArgs);
        }

        public override void OnNavigatedTo(NavigationArgs navigationArgs)
        {
            base.OnNavigatedTo(navigationArgs);
        }

        public override void OnNavigatingFrom(ref bool cancel)
        {
            base.OnNavigatingFrom(ref cancel);
        }
    }
}
